package main

import (
	"fmt"
	"sync"
	"time"
)

var waitR sync.WaitGroup

var lockR sync.RWMutex

func main() {
	waitR.Add(6)
	for i := 0; i < 5; i++ {
		go read()
	}
	go write()
	waitR.Wait()

}

// 减操作
func read() {
	defer waitR.Done()
	lockR.RLock()
	fmt.Println(time.Now().Format(time.StampNano), "读操作开始")
	time.Sleep(time.Second)
	fmt.Println(time.Now().Format(time.StampNano), "读操作完成")
	lockR.RUnlock()
}

// 加操作
func write() {
	defer waitR.Done()
	lockR.Lock()
	fmt.Println(time.Now().Format(time.StampNano), "修改操作开始")
	time.Sleep(time.Second)
	fmt.Println(time.Now().Format(time.StampNano), "修改操作完成")
	lockR.Unlock()
}
